export default {
    data() {
        return {
            handleContextMenuStatus: true,
            columnsList: [],
            customCacheKey: ""
        }
    },
    methods: {
        handleContextMenuToggle() {

        },
        showMessageStatus(res){
            if (res.code==200) {
                this.$Message.success({
                    content: res.msg,
                    duration: 6,
                    closable: true,
                });
            }else{
                this.$Message.warning({
                    content: res.msg,
                    duration: 6,
                    closable: true,
                });
            }
        },
        concatArrAndOne(data,selectArr,id){
            const array1=JSON.parse(JSON.stringify(data))
            const array2=JSON.parse(JSON.stringify(selectArr))
            const mergedArray = array1.concat(array2).reduce((acc, obj) => {
                const found = acc.find(item => item[id] === obj[id]);
                if (!found) {
                  acc.push(obj);
                }
                return acc;
              }, []);
              return mergedArray;
        },
        getTableColumnsWidth(columns, customCacheKey) {
            this.getColumnWidth(columns, customCacheKey)
            this.getTableColumns(columns,customCacheKey)
        },
        getTableColumns(columns,customCacheKey) {
            this.handleContextMenuStatus = !this.handleContextMenuStatus;
            let _columns = []
            if (this.handleContextMenuStatus) {
                _columns = columns.filter(item => {
                    return item.slot !== "action"
                })
            } else {
                _columns = columns
            }
            if (customCacheKey) {
                this[`${customCacheKey}List`]=_columns
            }else this.columnsList = _columns
            return _columns
        },
        getColumnWidth(columns, customCacheKey) {
            const fullPath = customCacheKey || this.customCacheKey || this.$route.fullPath;
            let mesTable = JSON.parse(localStorage.getItem('mesTable'))
            if (mesTable && mesTable[fullPath]) {
              mesTable[fullPath].forEach(item => {
                    const obj = columns.find(m => m.key === item.key)
                    if (obj) {
                        columns[columns.indexOf(obj)].width = item.width
                    }
                })
            }
            return columns;
        },
        onColumnWidthResize(newWidth, oldWidth, column, event, customCacheKey) {


            const fullPath = customCacheKey || this.customCacheKey || this.$route.fullPath;
            let mesTable = JSON.parse(localStorage.getItem('mesTable'))
            column.width = newWidth
            if (!mesTable) mesTable = {}
            if (mesTable && mesTable[fullPath]) {
                if (mesTable[fullPath].find(item => item.key === column.key)) {
                  mesTable[fullPath].forEach(item => {
                        if (item.key === column.key) {
                            item.width = newWidth
                        }
                    })
                } else {
                  mesTable[fullPath].push(column)
                }

            } else {
              mesTable[fullPath] = [column]
            }
            localStorage.setItem('mesTable', JSON.stringify(mesTable))
        },
        // setTableByRef(ref){
        //     const refs = this.$refs[ref]
        //     const fullPath = this.$refs[ref].$attrs['data-key']
        //     let columns = refs._data.allColumns
        //     let mesTable = JSON.parse(localStorage.getItem('mesTable'))
        //     if (mesTable && mesTable[fullPath]) {
        //         mesTable[fullPath].forEach(item => {
        //             const obj = columns.find(m => m.key === item.key)
        //             if (obj) {
        //                 columns[columns.indexOf(obj)].width = item.width
        //             }
        //         })
        //     }
        //
        //
        //     console.log(columns)
        //
        // }
        getPath(tree, code) {
            let path = [];
            const findPath = (tree, code, path) =>{
                for (var i = 0; i < tree.length; i++) {
                    const node = tree[i];
                    if (node.code == code) {
                        path.push(node.name)
                        return path
                    } else {
                        if (node.children && node.children.length) {
                            if (getPath(node.children, id, path)) {
                                path.push(node.value)
                                return path
                            }
                        }
                    }
                }
            };
            const fullPath = findPath(tree, code, path).resove().join('/')
            return fullPath;
        },
    }
}
